package product

// 列表类型迭代器&聚合器

// ListAggregate 列表聚合
type ListAggregate struct {
	Products []*Product
}

func NewListAggregate() *ListAggregate {
	return &ListAggregate{
		Products: make([]*Product, 0),
	}
}
func (r *ListAggregate) AddProduct(product *Product) *ListAggregate {
	r.Products = append(r.Products, product)
	return r
}

func (r *ListAggregate) CreateIterator() Iterator {
	return &ListIterator{
		Aggregate: r,
		Index:     0,
	}
}

// ListIterator 列表迭代器
type ListIterator struct {
	Aggregate *ListAggregate
	Index     int
}

func (r *ListIterator) HasNext() bool {
	return r.Index < len(r.Aggregate.Products)
}

func (r *ListIterator) Next() interface{} {
	if r.HasNext() {
		product := r.Aggregate.Products[r.Index]
		r.Index++
		return product
	}
	return nil
}

func (r *ListIterator) Reset() {
	r.Index = 0
}
